<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Widget Plugin 
 * 
 * Install this file as application/plugins/widget_pi.php
 * 
 * @version:     0.1
 * $copyright     Copyright (c) Wiredesignz 2009-03-24
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
class Widget
{
	private $data;
	private $widget;
	
	public function __call($method, $arguments)
	{
		if (!method_exists( $this->widget_model, $method) )
		{
			throw new Exception('Undefined method Widget_model::' . $method . '() called');
		}

		return call_user_func_array( array($this->widget_model, $method), $arguments);
	}	
	public function __get($var)	{	return get_instance()->$var; }
    public function __construct() 
	{
		$this->load->model('widget_model');
	}
    
	
	protected function init_db($id)
	{
		$widget = $this->db->where('id', $id)->get('widgets');
		$this->widget = $widget;
		$result = $widgets->row_array();
		$this->data = json_decode($result['data'], TRUE);
	}
	

	protected function data($item = FALSE)
	{
		if($item === FALSE)
			return $this->data;
		else
			return $this->data[$item];
	}
	
	protected function set_data($item = array(), $value = FALSE)
	{
		if(is_array($item) && !empty($item) && $value === FALSE)
			$this->data = array_merge($this->data, $item);
		else
			$this->data[$item] = $value;			
	}
	
	protected function save()
	{
		return $this->update(array('id' => $this->widget['id'],
								   'data' => $this->data));
	
	}
    public function run($name) 
	{
		$data = $this->get(array('name' => $name));
		if(empty($data))
			return FALSE;
			
		$data = $data[0];      
		
		
		$args = array();
		$args[] = $data['id'];
		
		require_once APPPATH.'widgets/'.$data['file_name'].EXT;
		$name = ucfirst($data['file_name']);
		
		$widget =& new $name();
		return call_user_func_array(array(&$widget, 'run'), array_slice($args, 1));    
    }
    
    public function render($view, $data = array()) {
       // extract($data);
		$this->twiggy->set_theme('site');
		return $this->twiggy->display('widgets/'.$view, $data, TRUE);
        //include APPPATH.'widgets/views/'.$view.EXT;
    }
}  